Page({
    data: {
        name: '',
        phone: '',
        id: '',
        loginOK: false,
        flag: false,
        userimg: '/images/my.jpg',
    },

    onShow: function () {
        if (typeof this.getTabBar === 'function' && this.getTabBar()) {
            this.getTabBar().setData({
                selected: 2 //0,1，2 0-导航一  1-导航二  2-个人中心
            })
        }
        let user = wx.getStorageSync('user')
        if (user && user.name) {
            this.setData({
                loginOK: true,
                name: user.name,
                phone: user.phone,
                id: user._id,
                _openid: user._openid
            })
            if (user.userimg) {
                this.setData({
                    userimg: user.userimg
                })
            }
        } else {
            this.setData({
                loginOK: false
            })
        }
    },

    //退出登陆
    tuichu() {
        wx.setStorageSync('user', null)
        let user = wx.getStorageSync('user')
        if (user && user.name) {
            this.setData({
                loginOK: true,
                name: user.name
            })
        } else {
            this.setData({
                loginOK: false
            })
        }
    },

    onLoad: function (options) {

        // 这里的 name："home" 就是在 package.json中定义的名称
        // wx.cloud.callFunction({
        //     name: "home",
        // }).then(res =>{
        //     console.log(res)
        // }).catch(err =>{
        //     console.log(err)
        // })
    },
    //进入登录页
    url: function () {
        wx.navigateTo({
            url: '/mypages/sign/sign',
            //   events: events,
            success: (result) => { },
            fail: (res) => { },
            complete: (res) => { },
        })
    },
    //进入收藏页
    urlcoll: function () {
        wx.navigateTo({
            url: '/mypages/mycoll/mycoll',
            //   events: events,
            success: (result) => { },
            fail: (res) => { },
            complete: (res) => { },
        })
    },
    //进入我的发布页
    urlpub: function () {
        wx.navigateTo({
            url: '/mypages/mypublish/mypublish',
            //   events: events,
            success: (result) => { },
            fail: (res) => { },
            complete: (res) => { },
        })
    },
    //关于我们
    urlabout: function () {
        wx.navigateTo({
            url: '/mypages/about/about',
            //   events: events,
            success: (result) => { },
            fail: (res) => { },
            complete: (res) => { },
        })
    },
    //进入管理员页
    urladmin: function () {
        wx.navigateTo({
            url: '/mypages/admin/admin',
            //   events: events,
            success: (result) => { },
            fail: (res) => { },
            complete: (res) => { },
        })
    },
    //出现
    show: function () {
        if (this.data.flag) {
            this.setData({ flag: false })
        }
        else {
            this.setData({ flag: true })
        }

    },
    //消失
    hide: function () {
        this.setData({ flag: false })
    },
    //上传头像
    uploadPhoto: function () {
        wx.chooseMedia({
            count: 1,
            mediaType: ['image', 'video'],
            sourceType: ['album', 'camera'],
            sizeType: ['original', 'compressed'],
            maxDuration: 30,
            camera: 'back',
            success: res => {
                wx.showLoading({
                    title: '上传中',
                });
                console.log(res.tempFiles[0])
                var photoTempPath = res.tempFiles[0].tempFilePath
                this.uploadPhotoToDatabase(photoTempPath)
            }
        })
    },

    uploadPhotoToDatabase: function (photoTempPath) {
        var that = this;
        let id = this.data.id;
        let phone = this.data.phone;
        wx.cloud.uploadFile({
            cloudPath: "photo/" + Date.now() + ".jpg",
            filePath: photoTempPath
        })
            .then(res => {
                console.log(res)
                this.setData({
                    userimg: res.fileID,
                });
                let fileID = res.fileID;
                //把图片存到my_user集合对应id中
                const db = wx.cloud.database();
                db.collection("my_user").where({
                    phone: phone,
                })
                    .update({
                        data: {
                            userimg: fileID,
                            // _openid:openid
                        }
                    }).then(res => {
                        that.Renew();
                        console.log(res);
                        wx.hideLoading({
                            success: (res) => { },
                        })
                    })
                db.collection("my_share").where({
                    phone: phone
                }).update({
                    data: {
                        userimg: fileID,
                    }
                }).then(res => {
                    console.log("更新成功", res);
                })
            })
    },
    //更新登录状态
    Renew: function () {
        let phone = this.data.phone;
        wx.cloud.database().collection('my_user').where({
            phone: phone
        }).get({
            success(res) {
                let user = res.data[0]
                //保存用户登陆状态
                wx.setStorageSync('user', user)
            }
        })
    }

})